import threading
from doctest import debug
from threading import Thread
import time
import asyncio

class Utility:
    """
    工具类，包含一些常用的静态方法。
    """

    # 打印消息，包括当前时间与线程信息
    @staticmethod
    def print_info(info: any, include_current_time: bool = True, include_thread_info: bool = True,
                   include_task_info: bool = True, seperator: str = "\t", end_of_newline: bool = True) -> None:
        """
        打印消息。
        Args:
            info (any):消息文本。
            include_current_time (bool):是否包含当前时间。
            include_thread_info (bool):是否包含线程信息。
            include_task_info (bool): 是否包含任务信息。
            seperator (str):当前时间、线程信息、消息之间的分隔符。
            end_of_newline (bool):是否已换行结束。
        Returns:
            无。
        """
        ss: list[str] = list()
        if include_current_time:
            ss.append(Utility.get_current_time())
        if include_thread_info:
            current_thread: Thread = threading.current_thread()
            ss.append(f'{current_thread.name}({current_thread.ident})')
        if include_task_info:
            try:
                current_task: asyncio.Task = asyncio.current_task()
                if current_task is not None:
                    ss.append(f'{current_task.get_name()}')
            except RuntimeError:
                pass
        ss.append(str(info))
        s: str = seperator.join(ss)
        if end_of_newline:
            s = s + "\n"
        print(s)

    # 获取当前时间
    @staticmethod
    def get_current_time(length_of_ms: int = -1) -> str:
        """
        获取当前时间字符串。
        Args:
            length_of_ms (int):毫秒的位数。如果小于0，显示完整的毫秒；如果为0，不显示毫秒；如果大于0，显示指定位数的毫秒。
        Returns:
            当前时间字符串。
        """
        now_secs: float = time.time()
        now: time.struct_time = time.localtime(now_secs)
        ms: str
        if length_of_ms < 0:
            ms = str(now_secs % 1)[1:]
        elif length_of_ms == 0:
            ms = ''
        else:
            ms = str(now_secs % 1)[1:(1+length_of_ms)]
        return f'{now.tm_year}-{now.tm_mon}-{now.tm_mday} {now.tm_hour}:{now.tm_min}:{now.tm_sec}{ms}'